#!/bin/sh
# Install script for burncd.

# Copyright 2017-2018 orbea
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

IFS=' 	
'
\unset -f command printf unalias : 2>/dev/null
\unalias -a 2>/dev/null
PATH="$(command -p getconf PATH):$PATH"
LC_ALL=C; export LC_ALL

set -euf

cmd () {
  command=; print=
  while [ $# -gt 1 ]; do
    var="$1"; shift
    command="${command} $var"
    case "$var" in
      -- ) continue ;;
      * ) print="${print} $var" ;;
    esac
  done
  dir="$1"
  printf %s\\n " $print '$dir'"
  eval "set -- $command"
  command -p "$@" "$dir"
}

die () {
  ret="$1"; shift
  case "$ret" in
    : ) printf %s\\n "$@" >&2; return 0 ;;
    0 ) printf %s\\n "$@" ;;
    * ) printf %s\\n "$@" >&2 ;;
  esac
  exit "$ret"
}

case "${1:-}" in
  install ) INSTALL=1 ;;
  uninstall ) INSTALL=0 ;;
  '' ) die 1 'No install target.' ;;
  * ) die 1 "No rule to install target '$1'." ;;
esac

PREFIX=; BINDIR=; DOCDIR=; MANDIR=; DESTDIR=

PRGNAM=burncd
VERSION=0.0

shift

while [ $# -gt 0 ]; do
  arg="$1"; shift
  case "${arg%%=*}" in
    PREFIX|BINDIR|DOCDIR|MANDIR|DESTDIR ) eval "${arg%%=*}=\"\${arg#*=}\"" ;;
    * ) die : "WARNING: Variable '${arg%%=*}' was not used by $PRGNAM." ;;
  esac
done

PREFIX="${PREFIX:-/usr/local}"
BINDIR="${BINDIR:-${PREFIX}/bin}"
DOCDIR="${DOCDIR:-${PREFIX}/share/doc}/$PRGNAM-$VERSION"
MANDIR="${MANDIR:-${PREFIX}/share/man}/man1"
DESTDIR="${DESTDIR:-}"

for i in \$BINDIR \$DOCDIR \$MANDIR; do
  eval "dest=\"$i\""
  case "${dest:?}" in
    /*|${PREFIX}* ) : ;;
    * ) eval "${i#*$}=\"\${PREFIX}/$i\"" ;;
  esac
done

case $INSTALL in
  1 )
    printf %s\\n 'Installing...'
    cmd mkdir -p -- "${DESTDIR}${BINDIR}"
    cmd mkdir -p -- "${DESTDIR}${DOCDIR}"
    cmd mkdir -p -- "${DESTDIR}${MANDIR}"
    cmd rm -f -- "${DESTDIR}${BINDIR}/$PRGNAM"
    cmd cp -- $PRGNAM "${DESTDIR}${BINDIR}"
    cmd rm -f -- "${DESTDIR}${MANDIR}/$PRGNAM.1"
    cmd cp -- $PRGNAM.1 "${DESTDIR}${MANDIR}"
    cmd rm -f -- "${DESTDIR}${DOCDIR}/LICENSE"
    cmd cp -- LICENSE "${DESTDIR}${DOCDIR}"
    cmd rm -f -- "${DESTDIR}${DOCDIR}/README"
    cmd cp -- README "${DESTDIR}${DOCDIR}"
    cmd chmod 0755 -- "${DESTDIR}${BINDIR}/$PRGNAM"
  ;;
  0 )
    printf %s\\n 'Uninstalling...'
    cmd rm -rf -- "${DESTDIR}${DOCDIR}"
    cmd rm -f -- "${DESTDIR}${BINDIR}/$PRGNAM"
    cmd rm -f -- "${DESTDIR}${MANDIR}/$PRGNAM.1"
  ;;
esac

exit 0
